package com.ruicar.afs.cloud.afscase.approvetask.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.approvetask.entity.CaseApproveRecord;
import com.ruicar.afs.cloud.afscase.approvetask.service.CaseApproveRecordService;
import com.ruicar.afs.cloud.afscase.common.utils.DicUtils;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.ApproveTypeEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.UseSceneEnum;
import com.ruicar.afs.cloud.components.datadicsync.DicHelper;
import com.ruicar.afs.cloud.components.datadicsync.dto.DicDataDto;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @Description 审批记录
 * @Author Peng_Jinbo
 * @Date 2020/06/01 14:29
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/approveRecord")
public class ApproveRecordController {

    CaseApproveRecordService caseApproveRecordService;

    /**
     * @Description 查询置顶申请编号所有操作记录
     * @Author Peng_Jinbo
     * @Date 2020/6/1 14:30
     */
    @GetMapping("/listAllRecord")
    public IResponse listAllRecord(@RequestParam String applyNo) {
        List<String> useSceneValList = new ArrayList<>();
        useSceneValList.add(UseSceneEnum.APPROVE.getValue());
        useSceneValList.add(UseSceneEnum.CHANGE_ASSETS.getValue());
        useSceneValList.add(UseSceneEnum.RECONSIDER.getValue());
        useSceneValList.add(UseSceneEnum.GENERAL_LOAN.getValue());
        List<CaseApproveRecord> result = caseApproveRecordService.list(
                Wrappers.<CaseApproveRecord>lambdaQuery()
                        .eq(CaseApproveRecord::getApplyNo, applyNo)
                        .in(CaseApproveRecord::getUseScene, useSceneValList)
                        .orderByAsc(CaseApproveRecord::getId));
        // 翻译原因字典，拼接至审批建议
        for (CaseApproveRecord caseApproveRecord : result) {
            if (ApproveTypeEnum.REMIND.getValue().equals(caseApproveRecord.getApproveType())
                    || ApproveTypeEnum.PROCESS.getValue().equals(caseApproveRecord.getApproveType())) {
                // 翻译原因字典，拼接至审批建议
                transferReason(caseApproveRecord);
            }
        }
        return IResponse.success(result);
    }


    /**
     * @Description 翻译原因字典，拼接至审批建议
     * @Author Peng_Jinbo
     * @Date 2020/6/17 15:05
     */
    private void transferReason(CaseApproveRecord caseApproveRecord) {
        String approveReason = caseApproveRecord.getApproveReason();
        if (StrUtil.isNotBlank(approveReason)) {
            StringBuffer buffer = new StringBuffer();
            String[] reasonArr = approveReason.replaceAll("\\[", "")
                    .replaceAll("\\]", "")
                    .replaceAll("\"", "")
                    .split(",");
            Map<String, List<DicDataDto>> dicMap = DicHelper.getDicMaps(caseApproveRecord.getApproveSuggest());
            List<DicDataDto> dicDataDtoList = dicMap.get(caseApproveRecord.getApproveSuggest());
            if (CollectionUtil.isNotEmpty(dicDataDtoList)) {
                for (String reason : reasonArr) {
                    if (StrUtil.isNotBlank(reason)) {
                        buffer.append("【")
                                .append(DicUtils.translate(reason, dicMap))
                                .append("】,");
                    }
                }
            }
            caseApproveRecord.setApproveRemark(buffer + caseApproveRecord.getApproveRemark());
        }
    }
}
